// TankClient.java
package com.tankgame.server;

import com.tankgame.models.BaseTank;
import com.tankgame.models.PlayerTank;
import com.tankgame.panel.GamePanel;
import com.tankgame.utils.UniqueTankVector;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.Socket;
import java.util.Vector;

public class TankClient {
  private UniqueTankVector tanks;

  private DatagramSocket datagramSocket = null;
  private TankMsg tankMsg = null;
  private PlayerTank tank;
  private boolean connected = false;

  public TankClient(UniqueTankVector allTanks, PlayerTank tank) {
    this.tanks = allTanks;
    this.tank = tank;
  }

  //连接到服务器
  public void connect(String ip, int port) {
    Socket s = null;
    try {
      s = new Socket(ip, port);
      DataInputStream dis = new DataInputStream(s.getInputStream());
      int id = dis.readInt();
      this.tank.setId(id);
      System.out.println("Connected to server: " + s.getInetAddress() + " " + s.getPort());
      datagramSocket = new DatagramSocket(this.tank.getId());
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        if (s != null) {
          s.close();
        }
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    }

    //连接之后发送坦克信息到服务器同步
    if (tankMsg == null) {
      tankMsg = new TankMsg(tank);
      send(tankMsg);
    }

    //启动消息接收线程
    new Thread(new UDPReceiveThread()).start();
  }

  //发送方法
  public void send(TankMsg tankMsg) {
    System.out.println("Send a packet to server");
    tankMsg.sendTank(datagramSocket, TankServer.IP, TankServer.UDP_PORT);
  }

  public void sendMove(TankMsg tankMsg) {
    System.out.println("Send a packet to server");
    tankMsg.sendTank(datagramSocket, TankServer.IP, TankServer.UDP_PORT);
  }

  //接收消息线程
  private class UDPReceiveThread implements Runnable {
    byte[] buf = new byte[256 * 100];

    @Override
    public void run() {
      while (true) {
        while (datagramSocket != null) {
          DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length);
          try {
            datagramSocket.receive(datagramPacket);
            System.out.println("Receive a packet from server");

            //解析收到的消息
            parse(datagramPacket);

          } catch (IOException e) {
            throw new RuntimeException(e);
          }
        }
      }
    }

    //解析方法
    private void parse(DatagramPacket dp) {
      ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(buf, 0, dp.getLength());
      DataInputStream dis = new DataInputStream(byteArrayInputStream);
      tanks = tankMsg.parseAllTank(dis);
      for (int i = 0; i < tanks.size(); i++) {
        BaseTank t = tanks.get(i);
        if (t.getId() == tank.getId()) {
          tanks.remove(i);
          i--; // Adjust the index after removal
        }
      }
//      System.out.println(tanks.size());
      GamePanel.GetInstance().setAllTanks(tanks);
      GamePanel.GetInstance().addTankInAll(tank);
      GamePanel.GetInstance().repaint();


    }
  }
}